/**
 * Created by zhangyi on 2017/11/10.
 */
mainApp.factory("rewardFactory",function() {
    var service={};

    //修改时获取数据
    var object;
    service.setReward=function(data){
        object=data;

    }
    service.getReward=function(){
        return object;
    }

    service.getListData=function(currentindex){
        var obj={
            "pageIndex": currentindex,
            "pageSize": 8
        }
        var list="";
        RequestUtils.postJson("../reward/get",obj)
            .done(function(resp){
                if(resp.resultCode==0){
                    list=resp.data;
                    CommonUtils.buildFailHint("获取列表","成功");
                }else{
                    CommonUtils.buildFailHint("获取列表","失败");
                }
            });
        return list;
    }

    //新增奖惩数据的service
    service.addReward=function(rewardObj){
        console.log(rewardObj);

            RequestUtils.postForm('../reward/add', rewardObj).done(function (resp) {
                console.log(resp);
                if (resp.resultCode == 0) {
                    CommonUtils.buildSuccessHint('add', '成功');
                } else {
                    CommonUtils.buildFailHint('add', '失败');
                }
            });
        //}
    }

    //修改奖惩数据的service
    service.updateReward=function(rewardObj){
        RequestUtils.postForm('../reward/update', rewardObj)
            .done(function (resp) {
                console.log(resp);
                if (resp.resultCode == 0) {
                    CommonUtils.buildSuccessHint('update', '成功');
                } else {
                    CommonUtils.buildFailHint('update', '失败');
                }
            });
    }

    //根据工号(id)查询奖惩记录service
    service.selectRewardById=function(id){
        var list="";
        RequestUtils.getJson("../reward/get/"+id).done(function(resp){
            if(resp.resultCode==0){
                list=resp.data;
                CommonUtils.buildFailHint('记录查询', '成功');
            }else{
                CommonUtils.buildFailHint('记录查询', '失败');
            }
        });
        return list;
    }

    //根据工号查询奖惩记录service
    service.selectRewardByJobnum=function(jobnum,currentindex){
        var obj={
            "pageIndex": currentindex,
            "pageSize": 8
        }
        var list="";
        RequestUtils.postJson("../reward/getByJobnum/"+jobnum,obj).done(function(resp){
            console.log(resp);
            if(resp.resultCode==0){
                list=resp.data;
                CommonUtils.buildFailHint('记录查询', '成功');
            }else{
                CommonUtils.buildFailHint('记录查询', '失败');
            }
        });
        return list;
    }

    //删除奖惩记录service
    service.delete=function(id){
        RequestUtils.getJson("../reward/delete/"+id).done(function(resp){
            if(resp.resultCode==0){
                CommonUtils.buildSuccessHint('delete', '成功');
                location.reload(true);
            }else{
                CommonUtils.buildFailHint('delete', '失败');
            }
        })
    };

    return service;
});

mainApp.controller("rewardListController",function($scope,$http,rewardFactory,$state,overallSituationService) {
    overallSituationService.loginIntercept();

    $scope.rewardList=rewardFactory.getListData(1);
    $scope.list=$scope.rewardList.items;

    //查询工号，跳转修改页面
    $scope.selectReward=function(id){
        //添加标签
        var tab={url:"reward-update",text:"奖惩详情",id:"reward-update-tab",closeable:1};
        $scope.$emit('addTab', tab);

        var reward=rewardFactory.selectRewardById(id);
        rewardFactory.setReward(reward);
        $state.go('reward-update');
    }

    //删除日清记录
    $scope.delete=function(id){
        rewardFactory.delete(id);
    };

    $scope.addTab=function(){
        var tab={url:"reward-add",text:"奖惩新增",id:"reward-add-tab",closeable:1};
        $scope.$emit('addTab', tab);
    };

    $scope.listShow=true;
    $scope.listByWhereShow=false;

    $scope.selectWhere=function(selectByjobnum){
        console.log(selectByjobnum);
        $scope.listShow=false;
        $scope.listByWhereShow=true;
        $scope.rewardList=rewardFactory.selectRewardByJobnum(selectByjobnum.jobnum,1);
        $scope.list=$scope.rewardList.items;
    }

    //翻页
    $scope.NextListData=function(pageIndex){
        if($scope.listByWhereShow){
            $scope.rewardList=rewardFactory.selectRewardByJobnum($scope.PerEvalWhere,pageIndex);
        }else{
            $scope.rewardList=rewardFactory.getListData(pageIndex);
        }
        $scope.list=$scope.rewardList.items;
    }
});

mainApp.controller("rewardAddController",function($scope,$http,rewardFactory,$cookieStore) {
    //新增奖惩记录
    $scope.addReward=function(reward){
        console.log(reward.remark);
        console.log(reward.punishment);
        var rewardObj={
            "date":reward.date,
            "matter":reward.matter,
            "reward":reward.reward,
            "punishment":reward.punishment,
            "assessor":reward.assessor,
            "remark":reward.remark,
            "employee.name":reward.name
        };
        console.log(rewardObj);
        if(reward.date!="" && reward.date!=null
            && reward.matter!=""&&reward.matter!=null
            && reward.assessor!=""&&reward.assessor!=null
            && reward.name !=""&&reward.name!=null) {

            rewardFactory.addReward(rewardObj);
        }
    };

    CommonUtils.setDatetimepicker("datetimepicker");
});

mainApp.controller("rewardUpdateController",function($scope,$http,rewardFactory,$cookieStore) {
    //修改时查询出的数据列表
    $scope.updateRewardData=rewardFactory.getReward();

    //var rewardObj=$scope.updateRewardData;
    $scope.updateReward=function(reward){
        var rewardObj={
            "id":$scope.updateRewardData.id,
            "date":reward.date,
            "reward":reward.reward,
            "punishment":reward.punishment,
            "assessor":reward.assessor,
            "remark":reward.remark,
            "matter":reward.matter,
            "employee.id":$scope.updateRewardData.employee.id,
            "employee.name":reward.employee.name
        }

        if(reward.date!="" && reward.date!=null
            && reward.matter!=""&&reward.matter!=null
            && reward.assessor!=""&&reward.assessor!=null
            && reward.employee.name!=""&&reward.employee.name!=null) {

            rewardFactory.updateReward(rewardObj);
        }
    };

    CommonUtils.setDatetimepicker("datetimepicker");
});